﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Runtime.InteropServices;
using System.Drawing;
using SnapshotImage = System.Drawing.Image;

// Ooganizer namespaces
using Ooganizer.Platform;
using Ooganizer.Platform.Win32;
using Ooganizer.Resolver;
using Ooganizer.API;
using Ooganizer.Server;

namespace Ooganizer.Client.UI
{
    /// <summary>
    /// Interaction logic for ButtonWindow Form
    /// </summary>
    [ComVisible(false)]
    public partial class ButtonForm : Window
    {
        // Public Properties - IMP - Set/Passed into use by FormCreator BEFORE this window is actually loaded
        public BUTTON_HOOK_STATE ButtonState { get { return _buttonstate; } set { _buttonstate = value; } }
        public IntPtr ButtonWindow { get { return _hButtonWnd; } set { _hButtonWnd = value; } }
        public IntPtr HookedWindow { get { return _hHookedWnd; } set { _hHookedWnd = value; } }

        // Private member properties (for above)
        private IntPtr _hHookedWnd = IntPtr.Zero; 
        private BUTTON_HOOK_STATE _buttonstate = BUTTON_HOOK_STATE.BUTTON_NONE;
        private IntPtr _hButtonWnd = IntPtr.Zero;

        // Public Properties - IMP - Our Child Pages use those directly
        public Artifact Artifact { get { return _Artifact; } set { _Artifact = value; } }
        public string ArtifactTitle { get { return _ArtifactTitle; } set { _ArtifactTitle = value; } }
        public string ArtifactLocation { get { return _ArtifactLocation; } set { _ArtifactLocation = value; } }
        public string CurrentWorkspaceName { get { return _CurrentWorkspaceName; } set { _CurrentWorkspaceName = value; } }

        // Private member properties (for above)
        private Artifact _Artifact;
        private string _ArtifactTitle;
        private string _ArtifactLocation;
        private string _CurrentWorkspaceName;

        // Public Object Accessors
        public Env.Themes Theme { get { return _theme; } set { _theme = value; } }
        public GUICommon GUICommon { get { return _GUICommon; } set { _GUICommon = value; } }
        public ResolverDispatch ResolverDispatch { get { return _ResolverDisp; } set { _ResolverDisp = value; } }
        public SnapshotImage SnapshotImage { get { return _Image; } set { _Image = value; } }

        // Private obects (for above)
        private Env.Themes _theme = Env.Theme;
        private GUICommon _GUICommon = null;
        private ResolverDispatch _ResolverDisp = null;
        private SnapshotImage _Image = null;        

        // private Page Members
        ButtonFormPage_Add m_pageAdd = null;
        ButtonFormPage_Delete m_pageDelete = null;

        // Allow pages to query the frame height n' width
        public double FrameWidth { get { return frmButtonForm.Width; } }
        public double FrameHeight { get { return frmButtonForm.Height; } }

        /// <summary>
        /// Construct WPF ButtonForm :)
        /// </summary>
        public ButtonForm()
        {            
            InitializeComponent();
        }

        /// <summary>
        /// Window Load Event. We want all object loading to happen in the Load Event,
        /// because we construct the WPFForm and show it right away, so this is called right at
        /// start up, so put all the object loading in here to be cleaner and simplify FormObject Creator
        /// </summary>        
        private void OnLoad(object sender, RoutedEventArgs args)
        {
            try
            {
                _GUICommon = new GUICommon();
                _ResolverDisp = new ResolverDispatch();

                // Resolve the Window and set the properties accordingly, our pages below depend on this
                _Artifact = _ResolverDisp.GetArtifacts(_hHookedWnd).GetPrimary;
                _ArtifactTitle = _Artifact.Name;
                _ArtifactLocation = _Artifact.Location;

                using (OoganizerState oogyState = new OoganizerState())
                {
                    _CurrentWorkspaceName = oogyState.GetCurrentWorkspace();
                }

                switch (ButtonState)
                {
                    case BUTTON_HOOK_STATE.BUTTON_ADD:
                        m_pageAdd = new ButtonFormPage_Add();
                        m_pageAdd.ParentWPFContainer = this;
                        if (!frmButtonForm.Navigate(m_pageAdd))
                            Log.Info(string.Format("{0}() - ButtonForm could not navigate to PageAdd",
                                                   System.Reflection.MethodBase.GetCurrentMethod().Name));

                        break;

                    case BUTTON_HOOK_STATE.BUTTON_DELETE:
                        m_pageDelete = new ButtonFormPage_Delete();
                        m_pageDelete.ParentWPFContainer = this;
                        if (!frmButtonForm.Navigate(m_pageDelete))
                            Log.Info(string.Format("{0}() - ButtonForm could not navigate to PageDelete",
                                                   System.Reflection.MethodBase.GetCurrentMethod().Name));

                        break;

                    case BUTTON_HOOK_STATE.BUTTON_NONE:
                        Log.Error(string.Format("{0}() - ButtonForm is in Button_None ... this should not happen ... closing ButtonForm",
                                                System.Reflection.MethodBase.GetCurrentMethod().Name));

                        this.Close();
                        break;

                    default:
                        goto case BUTTON_HOOK_STATE.BUTTON_NONE;
                }
            }
            catch (Exception e)
            {
                Log.Error(string.Format("{0}() - A fatal error occured loading ButtonForm IMP",
                          System.Reflection.MethodBase.GetCurrentMethod().Name), e);
            }
        }

        #region log4net declaration

        private log4net.ILog Log
        {
            get
            {
                if (_logWrapper == null)
                {
                    _logWrapper = new LogServerClientWrapper(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
                }

                return _logWrapper.Log;
            }
        }

        private LogServerClientWrapper _logWrapper;

        #endregion
    }
}
